/*
 * Copyright 2009 XueSong Guo.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.webwheel.utils;

import cn.webwheel.di.Container;
import cn.webwheel.di.Key;
import cn.webwheel.di.NativeProvider;
import cn.webwheel.di.Provider;
import cn.webwheel.di.engine.DefaultContainer;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Properties;

/**
 * 以{@link java.util.Properties}为数据来源的参数注入提供者。
 */
public class PropProvider implements Provider {

    private Properties properties;

    public PropProvider(Properties properties) {
        this.properties = properties;
    }

    public PropProvider(File file) throws IOException {
        properties = new Properties();
        FileInputStream fis = new FileInputStream(file);
        try {
            properties.load(fis);
        } finally {
            fis.close();
        }
    }

    public Properties getProperties() {
        return properties;
    }

    @SuppressWarnings("unchecked")
    public void bind(Container container) {
        if (container instanceof DefaultContainer) {
            ((DefaultContainer)container).addNamedDesire(Prop.class.getSimpleName(), Prop.class);
        }
        container.bind(new Key(String.class, Prop.class.getSimpleName()), this);
        container.bind(new Key(boolean.class, Prop.class.getSimpleName()), this);
        container.bind(new Key(byte.class, Prop.class.getSimpleName()), this);
        container.bind(new Key(char.class, Prop.class.getSimpleName()), this);
        container.bind(new Key(short.class, Prop.class.getSimpleName()), this);
        container.bind(new Key(int.class, Prop.class.getSimpleName()), this);
        container.bind(new Key(long.class, Prop.class.getSimpleName()), this);
        container.bind(new Key(float.class, Prop.class.getSimpleName()), this);
        container.bind(new Key(double.class, Prop.class.getSimpleName()), this);
    }

    public Object get(Key key, String data, NativeProvider nativeProvider) {
        String s = properties.getProperty(data);
        Type type = key.getType();
        if (type == String.class) {
            return s;
        }
        if (type == int.class) {
            return Integer.valueOf(s);
        }
        if (type == double.class) {
            return Double.valueOf(s);
        }
        if (type == boolean.class) {
            return Boolean.valueOf(s);
        }
        if (type == long.class) {
            return Long.valueOf(s);
        }
        if (type == float.class) {
            return Float.valueOf(s);
        }
        if (type == byte.class) {
            return Byte.valueOf(s);
        }
        if (type == char.class) {
            return s.substring(0, 1);
        }
        if (type == short.class) {
            return Short.valueOf(s);
        }
        return null;
    }
}
